//
//  FillingViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/16.
//

#import "FillingViewController.h"
#import <FSTextView/FSTextView.h>
#import "BeFondViewController.h"
@interface FillingViewController ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic , strong) UIImage * image;
@property (nonatomic ,strong) SQCustomButton * btnHeader;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic , strong) FSTextView * fsTextView;
@end

@implementation FillingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)SQ_UIViewForSelfView{
    UIImageView * image =  [[UIImageView alloc]init];
     image.image = SQGetImage(@"背景1");
     image.contentMode = UIViewContentModeScaleAspectFill;
     [self.view addSubview:image];
     [image mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self.view);
     }];
    UILabel * label = [[UILabel alloc]init];
    label.textColor = UIColor.whiteColor;
    label.font = FONT_BOLD(24);
    label.numberOfLines = 0;
    label.text = SQNSLocalString(@"第一步\n填写你的资料吧");
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(100);
    }];
    [self.view addSubview:self.btnHeader];
    [self.btnHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(116);
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(label.mas_bottom).offset(30);
    }];
    UIImageView * xiangji =  [[UIImageView alloc]init];
    xiangji.image = SQGetImage(@"相机");
    xiangji.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:xiangji];
    [xiangji mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btnHeader.mas_right);
        make.bottom.equalTo(self.btnHeader.mas_bottom);
        make.size.mas_equalTo(36);
    }];
    WS(weakSelf);
    [self.btnHeader setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
    UILabel * nicheng = [[UILabel alloc]init];
    nicheng.text = SQNSLocalString(@"昵称");
    nicheng.font = FONT_MEDIUM(12);
    nicheng.textColor = SQColor(@"#D3D4D6");
    [self.view addSubview:nicheng];
    [nicheng mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btnHeader.mas_bottom).offset(24);
        make.left.equalTo(self.view).offset(20);
    }];
    
    UIImageView * left =  [[UIImageView alloc]init];
    left.image = SQGetImage(@"输入边框");
    left.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:left];
    [left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicheng);
        make.top.equalTo(nicheng.mas_bottom).offset(12);
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(32);
    }];
    
    UIImageView * right =  [[UIImageView alloc]init];
    right.image = SQGetImage(@"输入边框2");
    right.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:right];
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicheng).offset(150);
        make.top.equalTo(nicheng.mas_bottom).offset(12);
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(32);
    }];
    [self.view addSubview:self.fsTextView];
    [self.fsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(left);
        make.left.equalTo(left.mas_right);
        make.right.equalTo(right.mas_left);
        make.height.mas_equalTo(32);
    }];
    
    HeaderView * btn = [[HeaderView alloc]init];
    btn.font = FONT_BOLD(16);
    btn.text = SQNSLocalString(@"下一步");
    btn.layer.cornerRadius = 25;
    btn.headerImage.image = SQGetImage(@"按钮背景");
    [btn.btn addTarget:self action:@selector(SQ_buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-80);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.centerX.equalTo(self.view);
    }];
}
-(void)SQ_buttonClick{
//    if (!self.image) {
//        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请设置头像!")];
//        return;
//    }
//    if (![self.fsTextView.formatText isNotEmpty]) {
//        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请设置昵称!")];
//        return;
//    }
    BeFondViewController * vc = [[BeFondViewController alloc]init];
    vc.image = self.image;
    vc.jsonData = [[NSMutableDictionary alloc]init];
    if ([self.fsTextView.formatText isNotEmpty]) {
        [vc.jsonData setObject:self.fsTextView.formatText forKey:@"name"];
    }
    [self SQ_pushViewControllerFull:vc];
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"设置头像") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [self presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [self presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf SQ_caijian:photos.firstObject ];
    }];
}

-(void)SQ_caijian:(UIImage*)image {
    self.btnHeader.SQBackimageView.image = image;
    self.image = image;
}


- (SQCustomButton *)btnHeader{
    if (!_btnHeader) {
        _btnHeader = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(116, 116) midmargin:0];
        _btnHeader.SQBackimageView.image = SQGetImage(@"头像");
        _btnHeader.layer.cornerRadius = 58;
        _btnHeader.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _btnHeader.layer.masksToBounds = YES;
        _btnHeader.clipsToBounds = YES;
    }
    return _btnHeader;
}

-(FSTextView*)fsTextView{
    if (!_fsTextView) {
        _fsTextView = [FSTextView textView];
        _fsTextView.font = FONT_MEDIUM(18);
        _fsTextView.textColor = UIColor.whiteColor;
        _fsTextView.placeholder =SQNSLocalString(@"请输入你的昵称");
        _fsTextView.placeholderColor = SQColor(@"#B2B2B2");
        _fsTextView.backgroundColor = UIColor.clearColor;
        _fsTextView.textAlignment = NSTextAlignmentCenter;
        _fsTextView.maxLength = 10;
       
    }
    return _fsTextView;
}


@end
